﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SRCBQuestionnaireStatistic
{
    public static class Prompt
    {
        private static Form prompt = null;
        public static Form CurrentOpeningForm { get { return prompt; } }
        public static bool CurrentOpeningForm_UserClickedOKButton { get { return (bool)prompt.Tag; } }
        public static string ShowDialog(string text, string caption, int width, int height)
        {
            prompt = new Form();
            prompt.Width = width;
            prompt.Height = height;
            prompt.Text = caption;
            prompt.FormClosing += new FormClosingEventHandler(prompt_FormClosing);
            prompt.Tag = false; // "false" means user click "X" to close;
            Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
            TextBox textBox = new TextBox() { Left = 180, Top = 20, Width = 100 };
            textBox.Name = "txtaaa";
            Button confirmation = new Button() { Text = "Ok", Left = 310, Width = 80, Top = 20 };
            confirmation.Click += (sender, e) => {
                prompt.Tag = true;
                prompt.Close(); 
            
            };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(textBox);
            prompt.ShowDialog();
            return textBox.Text;
        }
        private static bool ValidCheck(string headerName)
        {
            if (string.IsNullOrEmpty(headerName))
                return false;
            if (headerName.Contains("\\") ||
                headerName.Contains("/") ||
                headerName.Contains(":") ||
                headerName.Contains("*") ||
                headerName.Contains("?") ||
                headerName.Contains("\"") ||
                headerName.Contains("|") ||
                headerName.Contains(">") ||
                headerName.Contains("<"))
                return false;

            return true;
        }
        private static void prompt_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form form = (Form)sender;
            if ((bool)form.Tag)
            {
                TextBox tb = (TextBox)form.Controls["txtaaa"];
                if (!ValidCheck(tb.Text))
                {
                    e.Cancel = true;
                    form.Tag = false; // reset the clicking status.
                    MessageBox.Show(string.Format("不支持的名字"), "错误");
                }
            }
        }
        public static string ShowDialog(string text, string caption)
        {
            return ShowDialog(text, caption, 500, 100);
        }

        //private static bool ValidCheck(string text)
        //{
        //    bool isValid = true;

        //    isValid = !string.IsNullOrEmpty(text);
        //    if (!isValid)
        //        MessageBox.Show("名字不能为空！请重新输入");

        //    return isValid;
        //}
    }
}
